<template>
  <div class="tableList">
    <div class="add">
      <el-button
        size="mini"
        icon="el-icon-refresh"
        v-if="showBtn"
        @click="changeMatters"
      >
        修改当前配置
      </el-button>
      <el-button
        size="mini"
        icon="el-icon-refresh"
        v-if="showBtn"
        @click="updateMatters"
      >
        恢复默认配置
      </el-button>
      <el-button
        size="mini"
        icon="el-icon-refresh"
        v-if="showBtn"
        @click="synchroMatters"
      >
        同步历史数据
      </el-button>
    </div>
    <div>
      <base-table :tableSetting="matterConfig"></base-table>
    </div>
    <dialogPublic
      title="编辑内容"
      :show.sync="show"
      @doSubmit="addMatter"
      width="40%"
      @doClose="() => (show = false)"
    >
      <div class="matterContent">
        <el-input
          type="textarea"
          :rows="20"
          maxlength="1000"
          placeholder="请输入内容"
          v-model="textarea"
        >
        </el-input>
        <div class="bottom">
          <span>提示：默认为上一次填写的内容，可以修改</span>
          <el-button @click="reslove" size="mini">恢复默认</el-button>
        </div>
      </div>
    </dialogPublic>
  </div>
</template>

<script>
import { queryShiftTips } from "@/api/operationManagement/handoverManagement.js";
import { reserveMatter } from "@/api/operationManagement/handoverMatter.js";
import {
  addOrUpdateMatter
} from "@/api/operationManagement/handoverMatter.js";
export default {
  props: {
    tableData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    tipParams: {
      type: Object,
      default: () => {
        return {};
      },
    },
    shiftId: {
      type: String,
      default: "",
    },
    status: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      show: false,
      textarea: "",
      showBtn: true,
      unEdit: false,
      currentRow: {},
      currentIndex: 0,
      //原始数据
      originData: [],
      matterConfig: {
        indexShow: true,
        highlightRow: false,
        column: [
          {
            label: "一级项",
            prop: "firstLevel",
            width: 100,
          },
          {
            label: "二级项",
            prop: "secondLevel",
            width: 200,
          },
          {
            label: "内容",
            prop: "content",
            type: "textareaBtn",
            hide: false,
            method: (row, index) => {
              if (!this.unEdit) {
                this.currentRow = row;
                this.currentIndex = index;
                this.textarea = row.content;
                this.show = true;
              }
            },
          },
        ],
        data: [],
        noBorderRight: false,
        autoHeight: true,
        spanMethod: ({ row, column, rowIndex, columnIndex }) => {
          if (columnIndex == 1) {
            return {
              rowspan: this.rowSpanArr[rowIndex],
              colspan: 1,
            };
          }
        },
        //height: 400,
      },
      rowSpanArr: [],
      position: 0,
    };
  },
  created() {
    switch (this.status) {
      case 0:
        this.showBtn = true;
        this.unEdit = false;
        break;
      case 1:
        this.showBtn = false;
        this.unEdit = true;
        break;
      default:
        this.showBtn = false;
        this.unEdit = false;
        break;
    }
  },
  watch: {
    tableData: {
      handler(val) {
        this.matterConfig.data = JSON.parse(JSON.stringify(val));
        this.getCheckRowSpan();
      },
      deep: true,
    },
  },
  methods: {
    //更新配置项
    updateMatters() {
      this.$confirm("此操作将会覆盖当前配置信息，是否继续", "", {
        confirmButtonText: "继续",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await queryShiftTips(this.tipParams);
          this.$message.success(res.description);
          this.matterConfig.data = res.data;
          this.getCheckRowSpan();
        })
        .catch(() => {});
    },
    //同步历史数据
    async synchroMatters() {
      let res = await reserveMatter({ recordId: this.shiftId });
      this.$message.success(res.description);
      this.matterConfig.data = res.data;
      this.getCheckRowSpan();
    },
    //恢复默认内容
    reslove() {
      this.textarea = this.tableData[this.currentIndex].content;
    },
    //配置
    changeMatters() {
      this.$emit("showMatterPage", true);
    },
    //检查项的合并规则
    getCheckRowSpan() {
      this.rowSpanArr = [];
      this.matterConfig.data.forEach((item, index) => {
        if (index == 0) {
          this.rowSpanArr.push(1);
          this.position = 0;
        } else {
          if (item.firstLevel == this.matterConfig.data[index - 1].firstLevel) {
            this.rowSpanArr[this.position] += 1; //项目名称相同，合并到同一个数组中
            this.rowSpanArr.push(0);
          } else {
            this.rowSpanArr.push(1);
            this.position = index;
          }
        }
      });
    },
    async addMatter() {
      this.currentRow.content = this.textarea;
      let  res = await addOrUpdateMatter(this.currentRow);
      this.$message.success(res.description);
      this.show = false;
    },
    getTableData() {
      return this.matterConfig.data;
    },
  },
};
</script>

<style lang="less" scoped>
.tableList {
  width: 100%;
  height: 100%;
  margin-top: 20px;
  .add {
    margin: 10px 0;
    text-align: right;
  }
  .matterContent {
    padding: 20px;
    .bottom {
      margin-top: 20px;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>